// TVSequenceProps.cpp : implementation file
//

#include "StdAfx.h"
#include "TVSequenceProps.h"
#include "IMovieSystem.h"
#include "TrackViewUndo.h"

#include "Objects/BaseObject.h"
#include "Objects/SequenceObject.h"

// CTVSequenceProps dialog

IMPLEMENT_DYNAMIC(CTVSequenceProps, CDialog)

CTVSequenceProps::CTVSequenceProps( IAnimSequence *seq,CWnd* pParent /* = NULL */ )
	: CDialog(CTVSequenceProps::IDD, pParent)
	, m_outOfRange(0)
{
	assert( seq );
	m_sequence = seq;
}

CTVSequenceProps::~CTVSequenceProps()
{
}

void CTVSequenceProps::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_NAME, m_nameEdit);
	DDX_Control(pDX, IDC_ALWAYS_PLAY, m_alwaysPlayingBtn);
	DDX_Control(pDX, IDC_CUT_SCENE, m_cutSceneBtn);
	DDX_Control(pDX, IDC_DISABLEHUD, m_NoHUDBtn);
	DDX_Control(pDX, IDC_NOSEEK, m_NoSeekBtn);
	DDX_Control(pDX, IDC_TIMEWARP_IN_FIXEDTIMESTEP, m_timeWarpBtn);
	DDX_Control(pDX, IDC_EARLYMOVIEUPDATE, m_earlyMovieUpdateBtn);
	DDX_Control(pDX, IDC_NOABORT, m_NoAbortBtn);
	DDX_Control(pDX, IDC_DISABLEPLAYER, m_NoPlayerBtn);
	DDX_Control(pDX, IDC_16TO9, m_16To9);
	DDX_Control(pDX, IDC_DISABLESOUNDS, m_NoSoundsBtn);
	DDX_Control(pDX, IDC_MOVE_SCALE_KEYS, m_moveScaleKeysBtn);
	DDX_Radio(pDX, IDC_ORT_ONCE, m_outOfRange);
}


BEGIN_MESSAGE_MAP(CTVSequenceProps, CDialog)
END_MESSAGE_MAP()


// CTVSequenceProps message handlers

BOOL CTVSequenceProps::OnInitDialog()
{
	CDialog::OnInitDialog();

	string name = m_sequence->GetName();
	m_nameEdit.SetWindowText( name.c_str() );
	int seqFlags = m_sequence->GetFlags();
	
	if (seqFlags & IAnimSequence::PLAY_ONRESET)
		m_alwaysPlayingBtn.SetCheck( BST_CHECKED );
	else
		m_alwaysPlayingBtn.SetCheck( BST_UNCHECKED );

	if (seqFlags & IAnimSequence::CUT_SCENE)
		m_cutSceneBtn.SetCheck( BST_CHECKED );
	else
		m_cutSceneBtn.SetCheck( BST_UNCHECKED );

	if (seqFlags & IAnimSequence::NO_HUD)
		m_NoHUDBtn.SetCheck( BST_CHECKED );
	else
		m_NoHUDBtn.SetCheck( BST_UNCHECKED );

	if (seqFlags & IAnimSequence::NO_PLAYER)
		m_NoPlayerBtn.SetCheck( BST_CHECKED );
	else
		m_NoPlayerBtn.SetCheck( BST_UNCHECKED );

	if (seqFlags & IAnimSequence::IS_16TO9)
		m_16To9.SetCheck( BST_CHECKED );
	else
		m_16To9.SetCheck( BST_UNCHECKED );

	m_NoSoundsBtn.SetCheck( (seqFlags&IAnimSequence::NO_GAMESOUNDS) ? BST_CHECKED : BST_UNCHECKED );
	m_NoSeekBtn.SetCheck( (seqFlags&IAnimSequence::NO_SEEK) ? BST_CHECKED : BST_UNCHECKED );
	m_NoAbortBtn.SetCheck( (seqFlags&IAnimSequence::NO_ABORT) ? BST_CHECKED : BST_UNCHECKED );
	m_timeWarpBtn.SetCheck( (seqFlags&IAnimSequence::TIMEWARP_IN_FIXEDTIMESTEP) ? BST_CHECKED : BST_UNCHECKED );
	m_earlyMovieUpdateBtn.SetCheck( (seqFlags&IAnimSequence::EARLY_MOVIE_UPDATE) ? BST_CHECKED : BST_UNCHECKED );

	m_moveScaleKeysBtn.SetCheck( BST_UNCHECKED );

	m_startTime.Create( this,IDC_START_TIME );
	m_endTime.Create( this,IDC_END_TIME );
	
	m_startTime.SetRange( 0.0,(1e+10) );
	m_endTime.SetRange( 0.0,(1e+10) );

	Range timeRange = m_sequence->GetTimeRange();
	m_startTime.SetValue( timeRange.start );
	m_endTime.SetValue( timeRange.end );

	m_outOfRange = 0;
	if (m_sequence->GetFlags() & IAnimSequence::ORT_CONSTANT)
	{
		m_outOfRange = 1;
	}
	else if (m_sequence->GetFlags() & IAnimSequence::ORT_LOOP)
	{
		m_outOfRange = 2;
	}
	UpdateData(FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CTVSequenceProps::MoveScaleKeys()
{
	// Move/Rescale the sequence to a new time range.
	Range timeRangeOld = m_sequence->GetTimeRange();
	Range timeRangeNew;
	timeRangeNew.start = m_startTime.GetValue();
	timeRangeNew.end = m_endTime.GetValue();

	if (!(timeRangeNew == timeRangeOld))
	{
		m_sequence->AdjustKeysToTimeRange( timeRangeNew );
	}
}

void CTVSequenceProps::OnOK()
{
	UpdateData(TRUE);

	CString name;
	m_nameEdit.GetWindowText( name );
	if (name.IsEmpty())
	{
		AfxMessageBox( "A sequence name cannot be empty!",MB_OK|MB_ICONEXCLAMATION );
		return;
	}
	else if (name.Find('/') != -1)
	{
		AfxMessageBox( "A sequence name cannot contain a '/' character!",MB_OK|MB_ICONEXCLAMATION );
		return;
	}

	CUndo undo( "AnimSequence Modified" );
	CUndo::Record( new CUndoAnimSequenceObject(m_sequence) );

	if (m_moveScaleKeysBtn.GetCheck() == BST_CHECKED)
		MoveScaleKeys();

	Range timeRange;
	timeRange.start = m_startTime.GetValue();
	timeRange.end = m_endTime.GetValue();
	m_sequence->SetTimeRange( timeRange );

	string seqName = m_sequence->GetName();
	if (name != seqName.c_str())
	{
		// Rename sequence.
		m_sequence->SetName(name.GetBuffer());
	}

	int seqFlags = m_sequence->GetFlags();
	seqFlags &= ~(IAnimSequence::ORT_CONSTANT | IAnimSequence::ORT_LOOP);

	if (m_alwaysPlayingBtn.GetCheck() == BST_CHECKED)
		seqFlags |= IAnimSequence::PLAY_ONRESET;
	else
		seqFlags &= (~IAnimSequence::PLAY_ONRESET);

	if (m_cutSceneBtn.GetCheck() == BST_CHECKED)
		seqFlags |= IAnimSequence::CUT_SCENE;
	else
		seqFlags &= (~IAnimSequence::CUT_SCENE);

	if (m_NoHUDBtn.GetCheck() == BST_CHECKED)
		seqFlags |= IAnimSequence::NO_HUD;
	else
		seqFlags &= (~IAnimSequence::NO_HUD);

	if (m_NoPlayerBtn.GetCheck() == BST_CHECKED)
		seqFlags |= IAnimSequence::NO_PLAYER;
	else
		seqFlags &= (~IAnimSequence::NO_PLAYER);

	if (m_16To9.GetCheck() == BST_CHECKED)
		seqFlags |= IAnimSequence::IS_16TO9;
	else
		seqFlags &= (~IAnimSequence::IS_16TO9);
	
	if (m_outOfRange == 1)
		seqFlags |= IAnimSequence::ORT_CONSTANT;
	else if (m_outOfRange == 2)
		seqFlags |= IAnimSequence::ORT_LOOP;

	if (m_NoSoundsBtn.GetCheck() == BST_CHECKED)
		seqFlags |= IAnimSequence::NO_GAMESOUNDS;
	else
		seqFlags &= (~IAnimSequence::NO_GAMESOUNDS);
	
	if (m_NoSeekBtn.GetCheck() == BST_CHECKED)
		seqFlags |= IAnimSequence::NO_SEEK;
	else
		seqFlags &= (~IAnimSequence::NO_SEEK);

	if (m_NoAbortBtn.GetCheck() == BST_CHECKED)
		seqFlags |= IAnimSequence::NO_ABORT;
	else
		seqFlags &= (~IAnimSequence::NO_ABORT);

	if (m_timeWarpBtn.GetCheck() == BST_CHECKED)
		seqFlags |= IAnimSequence::TIMEWARP_IN_FIXEDTIMESTEP;
	else
		seqFlags &= (~IAnimSequence::TIMEWARP_IN_FIXEDTIMESTEP);

	if (m_earlyMovieUpdateBtn.GetCheck() == BST_CHECKED)
		seqFlags |= IAnimSequence::EARLY_MOVIE_UPDATE;
	else
		seqFlags &= (~IAnimSequence::EARLY_MOVIE_UPDATE);

	m_sequence->SetFlags( seqFlags );
	
	CDialog::OnOK();
}
